package com.stylefeng.guns.modular.pay.controller;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.state.ManagerStatus;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.RandomStringGenerator;
import com.stylefeng.guns.modular.pay.model.Account;
import com.stylefeng.guns.modular.pay.model.MchInfo;
import com.stylefeng.guns.modular.pay.service.IAccountService;
import com.stylefeng.guns.modular.pay.service.IMchInfoService;
import com.stylefeng.guns.modular.pay.service.impl.IdWorkerService;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.service.IUserService;

/**
 * 商户信息控制器
 *
 * @author fengshuonan
 * @Date 2018-08-24 15:17:20
 */
@Controller
@RequestMapping("/mchInfo")
public class MchInfoController extends BaseController {

    private String PREFIX = "/pay/mchInfo/";

    @Autowired
    private IMchInfoService mchInfoService;
    @Autowired
    private IdWorkerService idWorkerService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IAccountService accountService;

    /**
     * 跳转到商户信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "mchInfo.html";
    }

    /**
     * 跳转到添加商户信息
     */
    @RequestMapping("/mchInfo_add")
    public String mchInfoAdd() {
        return PREFIX + "mchInfo_add.html";
    }

    /**
     * 跳转到修改商户信息
     */
    @RequestMapping("/mchInfo_update/{mchInfoId}")
    public String mchInfoUpdate(@PathVariable Integer mchInfoId, Model model) {
        MchInfo mchInfo = mchInfoService.selectById(mchInfoId);
        model.addAttribute("item",mchInfo);
        LogObjectHolder.me().set(mchInfo);
        return PREFIX + "mchInfo_edit.html";
    }

    /**
     * 获取商户信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(MchInfo mchInfo) {
        return mchInfoService.selectList(null);
    }

    /**
     * 新增商户信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(MchInfo mchInfo) {
    	mchInfo.setCreateTime(new Date());
    	mchInfo.setSignType(0);
    	mchInfo.setStatus(1);
    	mchInfo.setMd5Key(RandomStringGenerator.getRandomStringByLength(32));//秘钥设置32位
    	String mchNo =idWorkerService.idGenerator();
    	mchInfo.setMchNo(mchNo);
        mchInfoService.insert(mchInfo);
        //添加登录帐号
     // 判断账号是否重复
        User theUser = userService.getByAccount(mchNo);
        if (theUser != null) {
            throw new GunsException(BizExceptionEnum.USER_ALREADY_REG);
        }
        User user = new User();
        // 完善账号信息
        user.setSalt(ShiroKit.getRandomSalt(5));
        user.setPassword(ShiroKit.md5("987654", user.getSalt()));
        user.setStatus(ManagerStatus.OK.getCode());
        user.setCreatetime(new Date());
        user.setRoleid("6");
        user.setName(mchInfo.getContactName());
        user.setEmail(mchInfo.getContactEmail());
        user.setPhone(mchInfo.getContactPhone());
        user.setAccount(mchNo);
        this.userService.insert(user);
        //新增帐号信息
        Account account = new Account();
        account.setAccountName(mchInfo.getMchName());
        account.setAccountNo("88"+idWorkerService.idGenerator());
        account.setAvailableAmount(BigDecimal.ZERO);
        account.setFrozenAmount(BigDecimal.ZERO);
        account.setMchNo(mchNo);
        account.setTotalAmount(BigDecimal.ZERO);
        account.setLastModifyTime(new Date());
        accountService.insert(account);
        return SUCCESS_TIP;
    }

    /**
     * 删除商户信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer mchInfoId) {
    	MchInfo info = mchInfoService.selectById(mchInfoId);
        mchInfoService.deleteById(mchInfoId);
        //删除登录帐号信息
        userService.deleteByAccount(info.getMchNo());
        return SUCCESS_TIP;
    }

    /**
     * 修改商户信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(MchInfo mchInfo) {
        mchInfoService.updateById(mchInfo);
        return SUCCESS_TIP;
    }

    /**
     * 商户信息详情
     */
    @RequestMapping(value = "/detail/{mchInfoId}")
    @ResponseBody
    public Object detail(@PathVariable("mchInfoId") Integer mchInfoId) {
        return mchInfoService.selectById(mchInfoId);
    }
}
